package com.peng.leetcode.array;

import java.util.*;

/**
 * AdvantageCount
 * 870. 优势洗牌
 * https://leetcode.cn/problems/advantage-shuffle/
 *
 * @author: lupeng6
 * @create: 2023/3/8 20:10
 */
public class AdvantageCount {
    public static void main(String[] args) {

    }

    public int[] advantageCount(int[] nums1, int[] nums2) {
        if (nums1 == null || nums1.length == 0) {
            return new int[0];
        }
        int[] ans = new int[nums1.length];
        Integer[] indexArr1 = new Integer[nums1.length];
        Integer[] indexArr2 = new Integer[nums1.length];
        for (int i = 0; i < nums1.length; i++) {
            indexArr1[i] = i;
            indexArr2[i] = i;
        }
        Arrays.sort(indexArr1, Comparator.comparingInt(a -> nums1[a]));
        Arrays.sort(indexArr2, Comparator.comparingInt(a -> nums2[a]));

        int l = 0;
        int r = nums2.length - 1;
        for (int i = 0; i < nums2.length; i++) {
            if (nums1[indexArr1[i]] > nums2[indexArr2[l]]) {
                ans[indexArr2[l]] = nums1[indexArr1[i]];
                l++;
            } else {
                ans[indexArr2[r]] = nums1[indexArr1[i]];
                r--;
            }
        }
        return ans;
    }
}
